using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestUniqueConstraintInfosControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }
        [Test]
        public void TestConstruction()
        {
            //---------------Set up test pack-------------------

            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsInstanceOfType(typeof(PanelWin), control);
            Assert.IsInstanceOfType(typeof(IFormControl), control);
            Assert.AreNotEqual(0, control.Controls.Count);
            IBOGridAndEditorControl gridAndBOEditorControl = UITestUtils.GetGridAndBOEditorControl(control);
            Assert.IsNotNull(gridAndBOEditorControl, "Should have a Grid and BO Editor Control");
        }

        [Test]
        public void TestSetBusinessObjectInfo_LoadsUniqueConstraintInfos()
        {
            //---------------Set up test pack-------------------
            DMClass info = TestUtils.CreateSavedDMClassWithUniqueConstraints();
            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            IBOGridAndEditorControl gridAndBOEditorControl = UITestUtils.GetGridAndBOEditorControl(control);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = info;
            //---------------Test Result -----------------------
            Assert.AreSame(info, control.Class);
            Assert.AreEqual(info.UniqueConstraints, control.UniqueConstraintInfos);
            IBusinessObjectCollection gridCollection = gridAndBOEditorControl.GridControl.Grid.BusinessObjectCollection;
            Assert.AreSame(gridCollection, control.UniqueConstraintInfos);
        }

        [Test]
        public void TestSetBusinessObject_ToNull_LoadsEmptyCollection()
        {
            //---------------Set up test pack-------------------
            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, control.UniqueConstraintInfos.Count);
        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            UniqueConstraintInfosControl control =
                new UniqueConstraintInfosControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetBusinessObject_EnablesControl()
        {
            //---------------Set up test pack-------------------
            DMClass info = TestUtils.CreateSavedDMClassWithUniqueConstraints();
            UniqueConstraintInfosControl control =
                new UniqueConstraintInfosControl(new ControlFactoryWin());

            //---------------Execute Test ----------------------
            control.Class = info;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void TestSetBusinessObject_ToNull_DisablesControl()
        {
            //---------------Set up test pack-------------------
            DMClass info = TestUtils.CreateSavedDMClassWithUniqueConstraints();
            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            control.Class = info;
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetUniqueConstraintInfo_SetsSelectedUniqueConstraintInfo()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtils.CreateSavedDMClassWithUniqueConstraints();
            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            IBOGridAndEditorControl gridAndBOEditorControl = UITestUtils.GetGridAndBOEditorControl(control);
            control.Class = dmClass;
            DMUniqueConstraint info = dmClass.UniqueConstraints[1];

            //---------------Execute Test ----------------------
            control.UniqueConstraint = info;

            //---------------Test Result -----------------------
            Assert.AreSame(info, control.UniqueConstraint);
            Assert.AreSame(info, gridAndBOEditorControl.CurrentBusinessObject);
        }

        [Test]
        public void TestLinkBetweenUniqueConstraintInfoAndUniqueConstraintPropertyInfoControls()
        {
            //---------------Set up test pack-------------------
            DMUniqueConstraint info = TestUtils.CreateSavedUniqueConstraintInfoWithProperties();
            //DMProperty propInfo = TestUtils.CreateSavedBusinessObjectPropertyInfo(boInfo);

            UniqueConstraintInfosControl control = new UniqueConstraintInfosControl(new ControlFactoryWin());
            control.Class = info.Class;

            //---------------Execute Test ----------------------
            control.UniqueConstraint = info;
            //---------------Test Result -----------------------
            Assert.AreSame(info, control.UniqueConstraintPropertyInfosControl.UniqueConstraint);
            //---------------Tear Down -------------------------          
        }
    }
}
